﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace TPCN.DAL
{
    public class DonGiaDAL
    {
        public static DonGiaObj GetByID(string DongiaID)
        {
            DonGiaObj obj = new DonGiaObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbDonGia_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@DongiaID", DongiaID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }
        public static List<DonGiaObj> GetByAll()
        {
            List<DonGiaObj> result = new List<DonGiaObj>();
            DonGiaObj obj = new DonGiaObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbDonGia_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }
        public static bool Insert(DonGiaObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDonGia_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@HangID", (obj.HangID == null) ? String.Empty : obj.HangID);
                    cmd.Parameters.AddWithValue("@Ngaybatdau", (obj.Ngaybatdau == null) ? String.Empty : obj.Ngaybatdau);
                    cmd.Parameters.AddWithValue("@Ngayketthuc", (obj.Ngayketthuc == null) ? String.Empty : obj.Ngayketthuc);
                    cmd.Parameters.AddWithValue("@NhanvienId", (obj.NhanvienID == null) ? String.Empty : obj.NhanvienID);
                    cmd.Parameters.AddWithValue("@Giathitruong", (obj.Giathitruong == null) ? "0" : obj.Giathitruong);
                    cmd.Parameters.AddWithValue("@Giabanle", (obj.Giabanle == null) ? "0" : obj.Giabanle);
                    cmd.Parameters.AddWithValue("@Giabanbuon", (obj.Giabanbuon == null) ? "0" : obj.Giabanbuon);
                    cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Update(DonGiaObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDonGia_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DongiaID", (obj.DongiaID == null) ? String.Empty : obj.DongiaID);
                    cmd.Parameters.AddWithValue("@HangID", (obj.HangID == null) ? String.Empty : obj.HangID);
                    cmd.Parameters.AddWithValue("@Ngaybatdau", (obj.Ngaybatdau == null) ? String.Empty : obj.Ngaybatdau);
                    cmd.Parameters.AddWithValue("@Ngayketthuc", (obj.Ngayketthuc == null) ? String.Empty : obj.Ngayketthuc);
                    cmd.Parameters.AddWithValue("@NhanvienId", (obj.NhanvienID == null) ? String.Empty : obj.NhanvienID);
                    cmd.Parameters.AddWithValue("@Giathitruong", (obj.Giathitruong == null) ? "0" : obj.Giathitruong);
                    cmd.Parameters.AddWithValue("@Giabanle", (obj.Giabanle == null) ? "0" : obj.Giabanle);
                    cmd.Parameters.AddWithValue("@Giabanbuon", (obj.Giabanbuon == null) ? "0" : obj.Giabanbuon);
                    cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Delete(string DongiaID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbDonGia_DeleteByID";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@DongiaID", DongiaID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
